Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Modules #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
385 changes: 189 additions & 196 deletions ts/src/ts/com/d_project/image/GIFImage.ts

Large diffs are not rendered by default.

65 changes: 33 additions & 32 deletions ts/src/ts/com/d_project/io/Base64.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
'use strict';
namespace com.d_project.io {
import Base64EncodeOutputStream from './Base64EncodeOutputStream';
import Base64DecodeInputStream from './Base64DecodeInputStream';
import ByteArrayOutputStream from './ByteArrayOutputStream';
import ByteArrayInputStream from './ByteArrayInputStream';

/**
* Base64
* @author Kazuhiko Arase
*/
export class Base64 {
/**
* Base64
* @author Kazuhiko Arase
*/
export default class {

constructor() {
throw 'error';
throw 'error';
}

public static encode(data : number[]) : number[] {
var bout = new ByteArrayOutputStream();
try {
var ostream = new Base64EncodeOutputStream(bout);
public static encode(data: number[]): number[] {
var bout = new ByteArrayOutputStream();
try {
ostream.writeBytes(data);
var ostream = new Base64EncodeOutputStream(bout);
try {
ostream.writeBytes(data);
} finally {
ostream.close();
}
} finally {
ostream.close();
bout.close();
}
} finally {
bout.close();
}
return bout.toByteArray();
return bout.toByteArray();
}

public static decode(data : number[]) : number[] {
var bout = new ByteArrayOutputStream();
try {
var istream = new Base64DecodeInputStream(
new ByteArrayInputStream(data) );
public static decode(data: number[]): number[] {
var bout = new ByteArrayOutputStream();
try {
var b : number;
while ( (b = istream.readByte() ) != -1) {
bout.writeByte(b);
var istream = new Base64DecodeInputStream(
new ByteArrayInputStream(data));
try {
var b: number;
while ((b = istream.readByte()) != -1) {
bout.writeByte(b);
}
} finally {
istream.close();
}
} finally {
istream.close();
bout.close();
}
} finally {
bout.close();
}
return bout.toByteArray();
return bout.toByteArray();
}
}
}
101 changes: 49 additions & 52 deletions ts/src/ts/com/d_project/io/Base64DecodeInputStream.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,72 @@
/// <reference path="InputStream.ts"/>
'use strict';
namespace com.d_project.io {
import InputStream from './InputStream';

/**
* Base64DecodeInputStream
* @author Kazuhiko Arase
*/
export class Base64DecodeInputStream extends InputStream {
/**
* Base64DecodeInputStream
* @author Kazuhiko Arase
*/
export default class Base64DecodeInputStream extends InputStream {

private buffer = 0;
private buflen = 0;

constructor(private istream : InputStream) {
super();
constructor(private istream: InputStream) {
super();
}

public readByte() : number {
public readByte(): number {

while (this.buflen < 8) {
while (this.buflen < 8) {

var c = this.istream.readByte();
var c = this.istream.readByte();

if (c == -1) {
if (c == -1) {

if (this.buflen == 0) {
return -1;
}
if (this.buflen == 0) {
return -1;
}

throw 'unexpected end of file./' + this.buflen;
throw 'unexpected end of file./' + this.buflen;

} else if (c == '='.charCodeAt(0) ) {
} else if (c == '='.charCodeAt(0)) {

this.buflen = 0;
return -1;
this.buflen = 0;
return -1;

} else if (Base64DecodeInputStream.isWhitespace(c) ) {
// ignore if whitespace.
continue;
}
} else if (Base64DecodeInputStream.isWhitespace(c)) {
// ignore if whitespace.
continue;
}

this.buffer = (this.buffer << 6) |
Base64DecodeInputStream.decode(c);
this.buflen += 6;
}
this.buffer = (this.buffer << 6) |
Base64DecodeInputStream.decode(c);
this.buflen += 6;
}

var n = (this.buffer >>> (this.buflen - 8) ) & 0xff;
this.buflen -= 8;
return n;
var n = (this.buffer >>> (this.buflen - 8)) & 0xff;
this.buflen -= 8;
return n;
}

private static isWhitespace(c : number) : boolean {
return c == '\v'.charCodeAt(0) ||
c == '\t'.charCodeAt(0) ||
c == '\r'.charCodeAt(0) ||
c == '\n'.charCodeAt(0);
private static isWhitespace(c: number): boolean {
return c == '\v'.charCodeAt(0) ||
c == '\t'.charCodeAt(0) ||
c == '\r'.charCodeAt(0) ||
c == '\n'.charCodeAt(0);
}

private static decode(c : number) : number {
if ('A'.charCodeAt(0) <= c && c <= 'Z'.charCodeAt(0) ) {
return c - 'A'.charCodeAt(0);
} else if ('a'.charCodeAt(0) <= c && c <= 'z'.charCodeAt(0) ) {
return c - 'a'.charCodeAt(0) + 26;
} else if ('0'.charCodeAt(0) <= c && c <= '9'.charCodeAt(0) ) {
return c - '0'.charCodeAt(0) + 52;
} else if (c == '+'.charCodeAt(0) ) {
return 62;
} else if (c == '/'.charCodeAt(0) ) {
return 63;
} else {
throw 'c:' + c;
}
private static decode(c: number): number {
if ('A'.charCodeAt(0) <= c && c <= 'Z'.charCodeAt(0)) {
return c - 'A'.charCodeAt(0);
} else if ('a'.charCodeAt(0) <= c && c <= 'z'.charCodeAt(0)) {
return c - 'a'.charCodeAt(0) + 26;
} else if ('0'.charCodeAt(0) <= c && c <= '9'.charCodeAt(0)) {
return c - '0'.charCodeAt(0) + 52;
} else if (c == '+'.charCodeAt(0)) {
return 62;
} else if (c == '/'.charCodeAt(0)) {
return 63;
} else {
throw 'c:' + c;
}
}
}
}
95 changes: 46 additions & 49 deletions ts/src/ts/com/d_project/io/Base64EncodeOutputStream.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,65 @@
/// <reference path="OutputStream.ts" />
'use strict';
namespace com.d_project.io {
import OutputStream from './OutputStream';

/**
* Base64EncodeOutputStream
* @author Kazuhiko Arase
*/
export class Base64EncodeOutputStream extends OutputStream {
/**
* Base64EncodeOutputStream
* @author Kazuhiko Arase
*/
export default class Base64EncodeOutputStream extends OutputStream {

private buffer = 0;
private buflen = 0;
private length = 0;

constructor(private ostream : OutputStream) {
super();
constructor(private ostream: OutputStream) {
super();
}

public writeByte(n : number) : void {
public writeByte(n: number): void {

this.buffer = (this.buffer << 8) | (n & 0xff);
this.buflen += 8;
this.length += 1;
this.buffer = (this.buffer << 8) | (n & 0xff);
this.buflen += 8;
this.length += 1;

while (this.buflen >= 6) {
this.writeEncoded(this.buffer >>> (this.buflen - 6) );
this.buflen -= 6;
}
while (this.buflen >= 6) {
this.writeEncoded(this.buffer >>> (this.buflen - 6));
this.buflen -= 6;
}
}

public flush() : void {
if (this.buflen > 0) {
this.writeEncoded(this.buffer << (6 - this.buflen) );
this.buffer = 0;
this.buflen = 0;
}
public flush(): void {
if (this.buflen > 0) {
this.writeEncoded(this.buffer << (6 - this.buflen));
this.buffer = 0;
this.buflen = 0;
}

if (this.length % 3 != 0) {
// padding
var padlen = 3 - this.length % 3;
for (var i = 0; i < padlen; i += 1) {
this.ostream.writeByte('='.charCodeAt(0) );
}
}
if (this.length % 3 != 0) {
// padding
var padlen = 3 - this.length % 3;
for (var i = 0; i < padlen; i += 1) {
this.ostream.writeByte('='.charCodeAt(0));
}
}
}

private writeEncoded(b : number) : void {
this.ostream.writeByte(Base64EncodeOutputStream.encode(b & 0x3f) );
private writeEncoded(b: number): void {
this.ostream.writeByte(Base64EncodeOutputStream.encode(b & 0x3f));
}

private static encode(n : number) : number {
if (n < 0) {
// error.
} else if (n < 26) {
return 'A'.charCodeAt(0) + n;
} else if (n < 52) {
return 'a'.charCodeAt(0) + (n - 26);
} else if (n < 62) {
return '0'.charCodeAt(0) + (n - 52);
} else if (n == 62) {
return '+'.charCodeAt(0);
} else if (n == 63) {
return '/'.charCodeAt(0);
}
throw 'n:' + n;
private static encode(n: number): number {
if (n < 0) {
// error.
} else if (n < 26) {
return 'A'.charCodeAt(0) + n;
} else if (n < 52) {
return 'a'.charCodeAt(0) + (n - 26);
} else if (n < 62) {
return '0'.charCodeAt(0) + (n - 52);
} else if (n == 62) {
return '+'.charCodeAt(0);
} else if (n == 63) {
return '/'.charCodeAt(0);
}
throw 'n:' + n;
}
}
}
33 changes: 15 additions & 18 deletions ts/src/ts/com/d_project/io/ByteArrayInputStream.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
/// <reference path="InputStream.ts" />
'use strict';
namespace com.d_project.io {
import InputStream from './InputStream';

/**
* ByteArrayInputStream
* @author Kazuhiko Arase
*/
export class ByteArrayInputStream extends InputStream {
/**
* ByteArrayInputStream
* @author Kazuhiko Arase
*/
export default class extends InputStream {

private pos = 0;

constructor(private bytes : number[]) {
super();
constructor(private bytes: number[]) {
super();
}

public readByte() : number {
if (this.pos < this.bytes.length) {
var b = this.bytes[this.pos];
this.pos += 1;
return b;
}
return -1;
public readByte(): number {
if (this.pos < this.bytes.length) {
var b = this.bytes[this.pos];
this.pos += 1;
return b;
}
return -1;
}
}
}
21 changes: 9 additions & 12 deletions ts/src/ts/com/d_project/io/ByteArrayOutputStream.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
/// <reference path="OutputStream.ts" />
'use strict';
namespace com.d_project.io {
import OutputStream from './OutputStream';

/**
/**
* ByteArrayOutputStream
* @author Kazuhiko Arase
*/
export class ByteArrayOutputStream extends OutputStream {
export default class extends OutputStream {

private bytes : number[] = [];
private bytes: number[] = [];

constructor() {
super();
super();
}

public writeByte(b : number) : void {
this.bytes.push(b);
public writeByte(b: number): void {
this.bytes.push(b);
}

public toByteArray() : number[] {
return this.bytes;
public toByteArray(): number[] {
return this.bytes;
}
}
}
Loading